home *** CD-ROM | disk | FTP | other *** search
- //*************** GLOBALS VARS *****************
-
-
- //******************* BEHAVIOR FUNCTIONS **********************
-
- //Changes multiple images at once. Accepts a variable number of args in triplets as follows:
- // objStrNS - Javascript object ref for Netscape (ex: document.layers['foo'].document.myImage)
- // objStrIE - JScript object reference for Internet Explorer (ex: document.all['myImage'])
- // imgURL - an image filename, URL encoded. (ex: file.gif, http://www.x.com/y.gif)
- //
- //Tests for browser, and uses the first object string for NS, the second for IE.
- //Sets the image src property to the new filename: document.myImage.src = file.gif.
- //Fails gracefully on older browsers by ensuring the the object exists.
- //If the image is in a layer, fixes the reference so it works. It doesn't hurt
- //to set image.src in a browser (IE3) even if nothing changes.
- //The rest of the code is to support another Action, Swap Image Restore.
- //Builds an array of the original src values and saves it to a global property,
- //in the form of theObj,theObj.src,.... Prevents overwriting these values if called
- //repeatedly, to ensure we use the original src file set in the HTML.
-
- function MM_swapImage() { //v2.0
- var i,j=0,objStr,obj,swapArray=new Array,oldArray=document.MM_swapImgData;
- for (i=0; i < (MM_swapImage.arguments.length-2); i+=3) {
- objStr = MM_swapImage.arguments[(navigator.appName == 'Netscape')?i:i+1];
- if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||
- (objStr.indexOf('document.all[') ==0 && document.all ==null))
- objStr = 'document'+objStr.substring(objStr.lastIndexOf('.'),objStr.length);
- obj = eval(objStr);
- if (obj != null) {
- swapArray[j++] = obj;
- swapArray[j++] = (oldArray==null || oldArray[j-1]!=obj)?obj.src:oldArray[j];
- obj.src = MM_swapImage.arguments[i+2];
- } }
- document.MM_swapImgData = swapArray; //used for restore
- }
-
-
-
- //Restores a set of images to their previous source files.
- //Accepts a variable number of params, in pairs read from a global property:
- // objStr - Javascript object ref for an image (ex: document.myImage)
- // imgURL - original image filename, URL encoded. (ex: file.gif, http://www.x.com/y.gif)
- //
- //This is the companion Action to Swap Image. Swap Image gathers the original
- //image src filenames, and writes document.MM_swapImageData before changing the images.
- //This sets the images src properties back to their original filenames:
- // document.myImage.src = file.gif.
-
- function MM_swapImgRestore() { //v2.0
- if (document.MM_swapImgData != null)
- for (var i=0; i<(document.MM_swapImgData.length-1); i+=2)
- document.MM_swapImgData[i].src = document.MM_swapImgData[i+1];
- }
-
-
-
- //Preloads multiple images files in order. Accepts a variable number of args
- //(each should be quoted):
- // imgURL - an image filename, URL encoded. (ex: file.gif, http://www.x.com/y.gif)
- //
- //Creates a new array of Image objects. With each one, it assigns an image source
- //from the argument list. These are downloaded essentially simultaneously into the
- //client cache. When the user needs a new image file (for example: they go to the
- //next web page), the browser should quickly find this image in the cache.
- //!!! IMPORTANT !!! This function is also defined in file "Preload Images.htm".
- //Any edits must be made there as well as here.
-
- function MM_preloadImages() { //v2.0
- if (document.images) {
- var imgFiles = MM_preloadImages.arguments;
- if (document.preloadArray==null) document.preloadArray = new Array();
- var i = document.preloadArray.length;
- with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!="#"){
- preloadArray[i] = new Image;
- preloadArray[i++].src = imgFiles[j];
- } }
- }
-
- //******************* API **********************
-
-
- //Checks for the existence of images.
- //If none exist, returns false so this Action is grayed out.
-
- function canAcceptBehavior(){
-
- var retVal = "";
- if (createObjRefs()) {
- retVal = "onMouseOver";
- }
- if (retVal)
- return retVal;
- return false;
- }
-
-
-
- //Returns Javascript functions to be inserted in HTML head with script tags.
-
- function behaviorFunction(){
- return "MM_preloadImages,MM_swapImgRestore,MM_swapImage"
- }
-
-
-
- //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
- //Gets list of imgSrcs from doc attribute. With each imgSrc, it gets the parallel
- //img name from select 'menu'. Each imgSrc & imgObj are embedded as args.
- function applyBehavior() {
- var retVal="",i,j,argList="",imgList="",fnArray,imgSrcArray,imgSrc,imgObj,imgObjIE,newName;
- var imgArray = new Array(),imgObjsArray=document.MM_imgObjsArray;
-
- imgSrcArray = document.MM_myImgSrcs; //get global list of imgSrcs
- for (i=0; i<imgSrcArray.length; i++) { //with each imgSrc
- imgSrc = imgSrcArray[i];
- if (imgSrc) { //if not empty
- if (argList) argList += ","; //if stuff already in list, add comma
- if (document.MM_NS_REFS[i].indexOf(REF_UNNAMED)==0){ //if the image is unnamed
- //from this array, get unique image name
- newName = getUniqueName("IMG","Image",imgObjsArray);
- imgObjsArray[i].setAttribute("name",newName); //rename image in document
- createObjRefs(); //re-create refs based on new image name
- }
- imgObjIE = document.MM_IE_REFS[i]; // get IE ref
- imgObjNS = document.MM_NS_REFS[i]; //get NS ref
- argList += "'"+escQuotes(imgObjNS)+"','"+escQuotes(imgObjIE)+"','"+escape(imgSrc)+"'";
- imgList += (imgList?",":"")+"'"+escape(imgSrc)+"'";
- imgArray[imgArray.length] = "'"+escape(imgSrc)+"'"; //add string to list
- }
- }
- if (!argList) retVal = MSG_NoImgsSelected;
- else { //OK
- //Add or remove MM_swapImgRestore() based on checkbox setting
- selObj = dreamweaver.getBehaviorElement();
- if (!selObj){
- selArr = dreamweaver.getSelection();
- selObj = dreamweaver.offsetsToNode(selArr[0],selArr[1]);
-
- }
- if (document.theForm.restore) {
- if (document.theForm.restore.checked) { //add restore to onMouseOut handler
- setHandler(selObj,'onMouseOut','MM_swapImgRestore()');
- } else { //remove it
- delHandler(selObj,'onMouseOut','MM_swapImgRestore');
- }
- }
-
- //Add or remove MM_preloadImages() based on checkbox setting
- var obj = dreamweaver.getDocumentDOM("document").body;
- if (document.theForm.preload.checked) { //add preload call to onLoad handler
- argList += ",\'"+document.preloadId+"\'"; //add uniqueName to main fn call
- setHandler(obj,"onLoad","MM_preloadImages("+imgList+",'"+document.preloadId+"')",document.preloadId);
- } else {
- delHandler(obj,"onLoad","MM_preloadImages",document.preloadId);
- }
-
- retVal = "MM_swapImage("+argList+")"; //create correct function call
- }
- return retVal
- }
-
-
- //Returns a dummy function call to inform Dreamweaver the type of certain behavior
- //call arguments. This information is used by DW to fixup behavior args when the
- //document is moved or changed.
- //
- //It is passed an actual function call string generated by applyBehavior(), which
- //may have a variable list of arguments, and this should return a matching mask.
- //
- //The return values are:
- // URL : argument could be a file path, which DW will update during Save As...
- // NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
- // IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
- // other...: argument is ignored
-
- function identifyBehaviorArguments(fnCallStr) {
- var argList, argArray, numArgGroups, i;
-
- argList = "";
- argArray = extractArgs(fnCallStr);
- numArgGroups = (argArray.length - 1) / 3; //args come in triplets
- for (i=0; i<numArgGroups; i++) { //with each NSobj,IEobj,URL triplet
- argList += ((argList)?",":"")+"NS4.0ref,IE4.0ref,DEP";
- }
- return argList;
- }
-
-
-
- //Given the original function call, this parses out the args and updates
- //the UI. Loops through each imgObj,imgSrc pair.
- //If imgObj already present in menu, stuff imgSrc in imgSrcArray. If imgObj
- //doesn't exist, add to menu, and extend imgSrcArray.
-
- function inspectBehavior(behFnCallStr){
- var argArray,imgSrcArray,found,numImgs,i,imgObj,imgSrc,j,imgObjNum;
-
- argArray = extractArgs(behFnCallStr);//get new list of imgObj,imgSrc pairs
- imgSrcArray = document.MM_myImgSrcs; //get the prior list of imgSrcs
- numImgs = document.MM_NS_REFS.length;
- for (i=1; i<(argArray.length-2); i+=3){ //with each imgObj,imgSrc pair
- imgObj=unescQuotes(argArray[i]);
- imgSrc=unescape(argArray[i+2]);
- found = false;
- for (j=0; j<numImgs; j++){ //check if imgObj is in ref list
- if (document.MM_NS_REFS[j] == imgObj) { //if imgObj there
- imgSrcArray[j] = imgSrc; //store imgSrc at that pos
- if (imgSrc) addStarToMenuItem(document.theForm.menu,j);//if non-empty, mark with *
- found = true;
- break;
- }
- }
- if (!found) alert(errMsg(MSG_ImgNotFound,imgObj,imgSrc)); //if image name not found
- }
- document.MM_myImgSrcs = imgSrcArray; //save updated imageSrc list
- displayImgSrc(); //load the imageSrc for selected image
-
- //Determine if preloading, get id
- if (i < argArray.length) document.preloadId = argArray[i]; //unique id exists, get it
- document.theForm.preload.checked = (argArray.length > i); //if preload, check box
-
- //If restore checkbox is available, see if MM_swapImgRestore() exists, and check the box
- var theObj = findObject("restoreOption");
- if (theObj) { //restore checkbox is a possibility
- var selObj=dreamweaver.getBehaviorElement();
- if (selObj && selObj.tagName != "A") selObj = selObj.parentNode; //move out to A tag if needed
- if (selObj && selObj.tagName == "A") if (document.theForm.restore) {
- document.theForm.restore.checked = getHandler(selObj,'onMouseOut','MM_swapImgRestore');
- }
- }
- }
-
-
-
- //Given the original function call, this parses out the args and updates
- //the code. If there's a preload id at the end of the arglist, deletes
- //the preload handler. If there's a swap restore call, deletes that.
-
- function deleteBehavior(behFnCallStr){
- var argArray,obj,selArr,selObj;
-
- //Maybe remove preload handler
- argArray = extractArgs(behFnCallStr);//get new list of imgObj,imgSrc pairs
- if ((argArray.length-1)%3 == 1) { //if extra arg
- document.preloadId = argArray[argArray.length-1]; //unique id exists, get it
- obj = dreamweaver.getDocumentDOM("document").body;
- if (obj.innerHTML.indexOf(document.preloadId) == -1) // if swap image not just moved
- delHandler(obj,"onLoad","MM_preloadImages",document.preloadId);
- }
-
- //Maybe remove swap restore handler
- selObj=dreamweaver.getBehaviorElement();
- if (selObj && selObj.tagName != "A") selObj = selObj.parentNode; //move out to A tag if needed
- if (selObj && selObj.tagName == "A") if (selObj.outerHTML.indexOf("MM_swapImage(") == -1) {
- delHandler(selObj,'onMouseOut','MM_swapImgRestore');
- }
- }
-
-
-
- //***************** LOCAL FUNCTIONS ******************
-
- //Load the select menu with image names.
- //Also sets the global property MM_myImgSrcs to the right num of items.
-
- function initializeUI(){
- var niceNameSrcArray, nameArray, i, selTag="";
- var imgSrcArray = new Array();
-
- //Determine if RESTORE is an option. If not, remove UI for it
- //the dreamweaver.getBehaviorTag() check ensures the checkbox
- //is not available if a behavior is attached to a timeline
- var removeCheckbox = false;
- if (!dreamweaver.getBehaviorTag() ) //if behavior is in a timeline
- removeCheckbox = true;
- else{
- if (dreamweaver.getBehaviorElement()) selTag = dreamweaver.getBehaviorElement().tagName;
- if (!selTag) selTag = getSelectionTag();
- if (selTag!="A" && selTag!="IMG") //if sel not A or IMG
- removeCheckbox = true;
- }
- if (removeCheckbox){
- var theObj = findObject("restoreOption");
- if (theObj) theObj.outerHTML = ""; //remove restoreOption checkbox
- }
-
-
- //Create unique ID in case of preload
- document.preloadId = "#"+((new Date()).getTime());
-
- //Build and load picklist of images
- createObjRefs();
- niceNameSrcArray = document.MM_NS_REFS;
- //Search for unreferenceable objects. <DIV id="foo"> is IE only, <LAYER> is NS only.
- //if REF_CANNOT found, return empty string, and use IE refs for nice namelist.
- for (i=0; i<document.MM_NS_REFS.length; i++) {
- if (document.MM_IE_REFS[i].indexOf(REF_CANNOT) == 0) {
- document.MM_IE_REFS[i] = ""; //blank it out
- }
- if (document.MM_NS_REFS[i].indexOf(REF_CANNOT) == 0) {
- document.MM_NS_REFS[i] = ""; //blank it out
- niceNameSrcArray = document.MM_IE_REFS; //use the IE list
- }
- }
- nameArray = niceNames(niceNameSrcArray,TYPE_Image);
- for (i=0; i<nameArray.length; i++){
- document.theForm.menu.options[i]=new Option(nameArray[i]); //load menu
- imgSrcArray[i] = "";
- }
- pickSelectedImage(); //if an image is selected, selects it in the picklist
- document.MM_myImgSrcs = imgSrcArray; //set global
-
- document.theForm.imgSrc.focus(); //set focus on textbox
- document.theForm.imgSrc.select(); //set insertion point into textbox
- }
-
-
-
- //Creates arrays of all images, including those in other frames.
- //If none found, returns false.
-
- function createObjRefs(){
- document.MM_NS_REFS = getAllObjectRefs("NS 4.0","IMG");
- document.MM_IE_REFS = getAllObjectRefs("IE 4.0","IMG");
-
- return (document.MM_NS_REFS.length >0)
- }
-
-
-
- //Given imageSrc in form, looks up the menu's selection number, and stores the
- //new imageSrc at that position in the global document property "MM_myImgSrcs".
-
- function storeImgSrc(){
- var newImgSrc, imgSrcArray, menuIndex, newMenuText;
-
- newImgSrc = document.theForm.imgSrc.value;
- imgSrcArray = document.MM_myImgSrcs; //get the prior list of imgSrcs
- menuIndex = document.theForm.menu.selectedIndex; //get index to swap
- imgSrcArray[menuIndex] = newImgSrc; //swap
- document.MM_myImgSrcs = imgSrcArray; //rewrite list
- if (newImgSrc) { //if non-empty, mark with *
- addStarToMenuItem(document.theForm.menu, menuIndex);
- } else { //nothing to store, strip off any previous star
- newMenuText = stripStar(document.theForm.menu.options[menuIndex].text); //remove if old star
- document.theForm.menu.options[menuIndex]=new Option(newMenuText); //add new line to menu
- }
- document.theForm.menu.selectedIndex = menuIndex; //reset selection index
- }
-
-
-
- //Looks at the menu of names, and returns the imgSrc associated with the
- //selected item. Example: if the 2nd menu item's selected, returns 2nd item
- //stored in property "MM_myImgSrcs".
-
- function displayImgSrc(){
- var imgSrcArray, curImageSrcNum, imgSrc;
-
- imgSrcArray = document.MM_myImgSrcs; //get the list of imgSrcs
- curImageSrcNum = document.theForm.menu.selectedIndex; //get index selected
- imgSrc = imgSrcArray[curImageSrcNum]; //lookup imgSrc
- document.theForm.imgSrc.value= imgSrc; //write into text field
- }
-
-
-
- //Invokes dialog to allow user to select filename. Puts value in text input.
-
- function browseFileAndStore(){
- var fileName;
- fileName = browseForFileURL("select", "", true); //returns a local filename
- if (fileName) {
- document.theForm.imgSrc.value = fileName;
- storeImgSrc();
- }
- }
-
-
- function pickSelectedImage(){
- var imgsArray = document.MM_imgObjsArray = createObjsArray("IMG");
- var arrLen = imgsArray.length;
- var selArr = dreamweaver.getSelection();
- var selObj = dreamweaver.offsetsToNode(selArr[0],selArr[1]);
-
- for (i=0;i<arrLen;i++){
- if (imgsArray[i]==selObj)
- document.theForm.menu.selectedIndex=i;
- }
- }
-
-
-
- //Creates a unique name for objs of tagName, using tagString
- //for instance: if tagString = Image, returns a name like Image1
- function getUniqueName(tagName,tagString,tagNameObjsArray){
- var frameListSize,objName,dupe=true,counter=1;
- var objsArray=arguments[2],objsArrayLen = objsArray.length;
-
- while (dupe==true){ //check new name against name of all other tagName objs
- dupe=false;
- objName = tagString + counter++;
- //iterates through possible names: tagName1, then tagName2, etc.
- for (i=0;dupe==false && i<objsArrayLen;i++){
- //if another object of this type has the same name
- if (objsArray[i].getAttribute("name") == objName)
- dupe=true; //then repeat the loop, trying a new name
- }
- }
- return objName; //return new name
-
- }
-
- //Returns an array of objects of tagName
- //If doc is in a frameset, searches all frames in parent
- function createObjsArray(tagName){
- var frameListLen,objsArray=new Array(),thisFrame;
- if (dreamweaver.getDocumentDOM('parent')){//if frames
- frameListLen = dreamweaver.getDocumentDOM('parent').getElementsByTagName('frame').length;
- for (i=0;i<frameListLen;i++){
- thisFrame = 'parent.frames[' + i + ']';
- objsArray = objsArray.concat(dreamweaver.getDocumentDOM(thisFrame).getElementsByTagName(tagName));
- }
- } else //if no frames
- objsArray = dreamweaver.getDocumentDOM("document").getElementsByTagName(tagName);
- return objsArray;
- }
-
- //*************** GENERIC DOM MANIPULATION FNS *****************
-
- //Returns a function call if exists in event handler.
- // obj - DOM object, such as dreamweaver.getDocumentDOM().body
- // eventName - "onLoad", "onClick" etc (not case sensitive)
- // fnName - "MM_preloadImages" etc.
- // optStr - (optional) function call must contain this string to be found
- //Given <TAG onEvent="aaa();bbb();ccc()">,
- //calling getHandler(tagObj,'onEvent','bbb') will
- //return "bbb()". Returns empty if event or fn don't exist.
-
- function getHandler(obj,eventName,fnName, optStr) {
- var eventStr,fnArray,i,theChunk,retVal = "";
- eventStr = obj.getAttribute(eventName);
- if (eventStr) { //find previous call, or add it
- fnArray = dreamweaver.getTokens(eventStr,";");
- for (i=0; i<fnArray.length; i++) { //look at each code chunk
- if (fnArray[i].indexOf(fnName+'(') != -1 && (!optStr || //fn call found
- fnArray[i].indexOf(optStr) != -1)) {
- retVal = fnArray[i]; break;
- } }
- }
- return retVal
- }
-
-
-
- //Replaces or adds a fn call to an event handler
- // obj - DOM object, such as dreamweaver.getDocumentDOM().body
- // eventName - "onLoad", "onClick" etc (not case sensitive)
- // fnCall - "myFun('arg1','arg2')" etc.
- // optStr - (optional) function call must contain this string to be found
- //Given <TAG onEvent="aaa();bbb();ccc()">,
- //calling setHandler(tagObj,'onEvent','bbb(1,2)') will
- //replace "bbb()" with the altered fn call. If the event
- //does not exist, adds it. It fn didn't exist, adds it to the
- //end of the list.
-
- function setHandler(obj,eventName,fnCall, optStr) {
- var eventStr,fnName,fnArray=new Array(),i=0;
- eventStr = obj.getAttribute(eventName);
- if (eventStr) { //if event exists
- fnName = fnCall.substring(0,fnCall.indexOf("("));
- fnArray = dreamweaver.getTokens(eventStr,";");
- for (i; i<fnArray.length; i++) //search for fnName
- if (fnArray[i].indexOf(fnName+'(') != -1 && (!optStr || //fn call found
- fnArray[i].indexOf(optStr) != -1)) break;
- }
- fnArray[i] = fnCall;
- obj.setAttribute(eventName,fnArray.join(";"));
- return true
- }
-
-
-
- //Deletes a fn call from an event handler
- // obj - DOM object, such as dreamweaver.getDocumentDOM().body
- // eventName - "onLoad", "onClick" etc (not case sensitive)
- // fnName - "MM_preloadImages" etc.
- // optStr - (optional) function call must contain this string to be found
- //Given <TAG onEvent="aaa();bbb();ccc()">,
- //calling delHandler(tagObj,'onEvent','bbb') will
- //remove "bbb();". If it is the last fn in the handler,
- //removes the event entirely.
-
- function delHandler(obj,eventName,fnName, optStr) {
- var eventStr,fnArray=new Array(),i=0,j;
- eventStr = obj.getAttribute(eventName);
- if (eventStr) { //if event exists
- fnArray = dreamweaver.getTokens(eventStr,";");
- for (i; i<fnArray.length; i++) { //look at each code chunk
- if (fnArray[i].indexOf(fnName+'(') != -1 && (!optStr || //fn call found
- fnArray[i].indexOf(optStr) != -1)) { //and, if given, optStr exists
- if (fnArray.length == 1) { //if last one, remove attribute
- obj.removeAttribute(eventName);
- } else { //pull out
- for (j=i; j<fnArray.length; j++) fnArray[j] = fnArray[j+1]; //shift array
- fnArray.length--;
- obj.setAttribute(eventName,fnArray.join(';'));
- }
- break;
- } }
- }
- return true
- }
-
-
-
- //Returns the tag for the current selection, such as
- //IMG, A, DIV etc. Always uppercase.
-
- function getSelectionTag() {
- var retVal = "";
- var selArr = dreamweaver.getSelection()
- var selObj=dreamweaver.offsetsToNode(selArr[0],selArr[1]);
- if (selObj && selObj.tagName) retVal = selObj.tagName;
- return retVal
- }
-
- //**************** GENERIC FUNCTIONS ****************
-
- //function extractArgs(behFnCallStr){
- //function stripStar(theStr) {
- //function addStarToMenuItem(theSelect,menuIndex) {
- //function escQuotes(theStr){
- //function unescQuotes(theStr){
- //function niceNames(objRefArray,objTypeStr) {
- //function nameReduce (objName) {
- //function errMsg() {
- //function findObject(objName, parentObj) {
-